package com.fescoservice.service;

import java.util.List;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fescoservice.dao.OperatorDAO;
import com.fescoservice.exception.FSException;
import com.fescoservice.model.Operator;
import com.fescoservice.util.Constants;

@Service
public class OperatorService {
	private static final Logger log = LogManager.getLogger(OperatorService.class);
	@Autowired
	private OperatorDAO operatorDAO;
	
	public void saveOperator(Operator user){	
		if(null!=user.getOperId() && !"".equals(user.getOperId())){	
			operatorDAO.merge(user);
		}else{
			operatorDAO.save(user);
		}
	}	
	public void deleteOperator(String id){		
		Operator user = operatorDAO.findById(id);
		operatorDAO.delete(user);
	}	
	
	public Long checkOperIdExists(String operId)  throws FSException{
		String sql = "SELECT COUNT(*) AS CNT FROM T_OPERATOR C WHERE C.OPER_ID = :operid";
		Long number = operatorDAO.countOperatorsById(sql,operId);
		log.debug("~~~~~~~~~~~~~checkOperIdExists~~~`"+number+"~~~~~~~~~~~~~~~~~~~~~");
		return number;
	}
	
	public Long countOperators(String type)  throws FSException{
		String sql = "SELECT COUNT(*) AS CNT FROM T_OPERATOR C ";
		if(null!=type){
		   sql+="WHERE C.OPER_TYPE = :type";
	   }
		return operatorDAO.countOperators(sql,type);
	}
	public List listOperators(String type,int first)  throws FSException{
		String hql = "from Operator o left join o.company as c ";
		if(null!=type){
		   hql+="where o.operType = :type";
		}
		return operatorDAO.listOperators(hql,type,first);
	}
	public List listOperators(String type)  throws FSException{
		List result = null;		
		if(null==type){
			result = operatorDAO.findAll();
		}else{
			result = operatorDAO.findByOperType(type);
		}
		return result;
	}
	public List getCompanies(String name)  throws FSException{
		List result = null;		
		
		//result = operatorDAO.findByOperType(name);
	
		return result;
	}
	public Operator getOperator(String id){		
		return operatorDAO.findById(id);
	}	
	public String getCompanyName(String id){		
		return "";
	}	
	
	
	/**
	 * list
	 * @param language
	 * @return
	 * @throws FSException 
	 */
	public List hrLogin(final String userId,final String userPass, final String operTyp) throws FSException{		
		StringBuffer sql = new StringBuffer("from Operator c where operId = :userId and operPassword = :userPass ");
		
		if(Constants.OPERATOR_ROLE_INNER.equals(operTyp)){
			sql.append(" and c.operRoleId in ('"+Constants.OPERATOR_ROLE_CEO+"','"+Constants.OPERATOR_ROLE_MANAGER+"','"+Constants.OPERATOR_ROLE_BUSSINESS_MAN+"')");
		}else{
			sql.append(" and c.operRoleId in ('"+Constants.OPERATOR_ROLE_HR_MANAGER+"','"+Constants.OPERATOR_ROLE_HR+"')");
		}
		
		List list = operatorDAO.hrLogin(sql.toString(),userId,userPass);
		return list;
	}
	
	
	/**
	 * list
	 * @param language
	 * @return
	 * @throws FSException 
	 */
	public List queryHRLogin(final String userId, final String operTyp) throws FSException{		
		StringBuffer sql = new StringBuffer("from Operator c where operId = :userId");
		if(Constants.OPERATOR_ROLE_INNER.equals(operTyp)){
			sql.append(" and c.operRoleId in ('"+Constants.OPERATOR_ROLE_CEO+"','"+Constants.OPERATOR_ROLE_MANAGER+"','"+Constants.OPERATOR_ROLE_BUSSINESS_MAN+"')");
		}else{
			sql.append(" and c.operRoleId in ('"+Constants.OPERATOR_ROLE_HR_MANAGER+"','"+Constants.OPERATOR_ROLE_HR+"')");
		}
		List list = operatorDAO.queryHRLogin(sql.toString(),userId);
		return list;
	}
	
	
}
